//
//  ZYTools.m
//  Awhile
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "ZYTools.h"
#import <AFNetworking.h>
@implementation ZYTools

+ (void)getUrl:(NSString *)url
          body:(id)body
        result:(ZYResult)result
    headerFile:(NSDictionary *)headerFile
       success:(void (^)(id result))success
       failure:(void (^)(NSError *error))failure
{
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    // 2.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 3.设置返回值类型
    switch (result) {
        case ZYData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZYJSON:
            break;
        case ZYXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 4.设置网络请求返回值所支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    // 让网络指示器旋转
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    // 5.发送网络请求
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
                        // 成功回调
                        success(responseObject);
                        
                    }

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
                        NSLog(@"%@", error);
                        // 失败回调
                        failure(error);
                        
                    }
        
    }];
    
}


+ (void)postUrl:(NSString *)url
           body:(id)body
         result:(ZYResult)result
   requestStyle:(ZYRequestStyle)requestStyle
     headerFile:(NSDictionary *)headerFile
        success:(void (^)(id result))success
        failure:(void (^)(NSError *error))failure
{
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[AFHTTPSessionManager manager];
    
    // 2.网络请求请求体Body的类型
    switch (requestStyle) {
        case ZYRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case ZYRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    // 3.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 4.网络请求返回值的数据类型
    switch (result) {
        case ZYData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZYJSON:
            break;
        case ZYXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    
    // 5.设置网络请求返回值支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript", nil]];
    
    

    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       // success(responseObject);
        
        
        if (responseObject) {
            success(responseObject);
            
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            //             NSLog(@"--------%@",path_doc);
            
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
            
            // 缓存的文件夹
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
            success(result);
        }
    }];
}


@end
